package jframes;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class AddRemoveTab{
  
	JTabbedPane tab;
  
	public static void main(String[] args){
		AddRemoveTab ar = new AddRemoveTab();
	}

	public AddRemoveTab(){
		JFrame frame = new JFrame("Add Remove Tab Frame");
		tab = new JTabbedPane(); 
		frame.add(tab, BorderLayout.CENTER);
		

  
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		JButton button = new JButton("Add Tab");
		button.addActionListener(new MyAction());
		panel.add(button);
		tab.add("Add Tab", panel);
		
//////////////////////////////////////Table Code////////////////////////////////////////////////////////		

		//Container content = frame.getContentPane();
		
		Object rows[][] = { { "AMZN", "Amazon", "67 9/16" },
		        { "AOL", "America Online", "68 3/4" },
		        { "BOUT", "About.com", "56 3/8" },
		        { "CDNW", "CDnow", "4 7/16" },
		        { "DCLK", "DoubleClick", "87 3/16" },
		        { "EBAY", "eBay", "180 7/8" },
		        { "EWBX", "EarthWeb", "18 1/4" },
		        { "MKTW", "MarketWatch", "29" },
		        { "TGLO", "Theglobe.com", "4 15/16" },
		        { "YHOO", "Yahoo!", "151 1/8" },
		        { "AOL", "America Online", "68 3/4" },
		        { "BOUT", "About.com", "56 3/8" },
		        { "CDNW", "CDnow", "4 7/16" },
		        { "DCLK", "DoubleClick", "87 3/16" },
		        { "EBAY", "eBay", "180 7/8" },
		        { "EWBX", "EarthWeb", "18 1/4" },
		        { "MKTW", "MarketWatch", "29" },
		        { "TGLO", "Theglobe.com", "4 15/16" },
		        { "YHOO", "Yahoo!", "151 1/8" },
		        { "AOL", "America Online", "68 3/4" },
		        { "BOUT", "About.com", "56 3/8" },
		        { "CDNW", "CDnow", "4 7/16" },
		        { "DCLK", "DoubleClick", "87 3/16" },
		        { "EBAY", "eBay", "180 7/8" },
		        { "EWBX", "EarthWeb", "18 1/4" },
		        { "MKTW", "MarketWatch", "29" },
		        { "TGLO", "Theglobe.com", "4 15/16" },
		        { "YHOO", "Yahoo!", "151 1/8" }
		        };
		
		 Object columns[] = { "Symbol", "Name", "Price" };
		 JTable table = new JTable(rows, columns);
		 JScrollPane scrollPane = new JScrollPane(table);
		 
		 JPanel Tpanel = new JPanel();
		 Tpanel.setLayout(new BorderLayout());
		 tab.add("Table", Tpanel);
		 
		 Tpanel.add(scrollPane, BorderLayout.CENTER);
		 
////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		JPanel panel1 = new JPanel();
		JButton button1 = new JButton("Remove Tab");
		button1.addActionListener(new MyAction());
		panel1.add(button1);
		tab.add("Remove Tab", panel1);
  
		frame.setSize(400, 400);
		frame.setVisible(true);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}//end of AddRemoveTab Constructor

  public class MyAction implements ActionListener{
  
	  public void actionPerformed(ActionEvent e){
		  String str = e.getActionCommand();
  
		  if(str.equals("Add Tab")){
			  String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
			  if(!st.equals("")){
				  JPanel panel2 = new JPanel();
				  JLabel label = new JLabel("Your program is working successfully.");
				  panel2.add(label);
				  tab.add(st, panel2);	
			  }
			  }
		  else if(str.equals("Remove Tab")){
			  tab.remove(tab.getTabCount()-1);
		  }
	  }//end of actionPerformed
	  
	  }//end of MyAction
  
  }//end of class